library(tidyverse)
library(estimatr)
library(metafor)
library(broom)
library(magrittr)
library(ggbeeswarm)

covariates <-
  "+ lucid_pid_7n +
  lucid_age +
  lucid_race +
  lucid_hhin +
  political_knowledge_pre +
  political_interest_pre +
  cognitive_reflection_pre +
  need_for_cognition_pre +
  extraversion_pre +
  agreeableness_pre +
  conscientiousness_pre +
  emotional_stability_pre +
  openness_to_experience_pre"


covariates_no_pid <-
  "lucid_age +
  lucid_race +
  lucid_hhin +
  political_knowledge_pre +
  political_interest_pre +
  cognitive_reflection_pre +
  need_for_cognition_pre +
  extraversion_pre +
  agreeableness_pre +
  conscientiousness_pre +
  emotional_stability_pre +
  openness_to_experience_pre"

all_panels_long <- "https://github.com/thomasjwood/cgptw22/raw/main/all_panels_long.rds" %>% 
  url %>% 
  gzcon %>% 
  readRDS

treatments_df <- "https://github.com/thomasjwood/cgptw22/raw/main/treatments_df.rds" %>% 
  url %>% 
  gzcon %>% 
  readRDS

targets_long <- "https://github.com/thomasjwood/cgptw22/raw/main/ft_targets_long.csv" %>% 
  read_csv

filter_and_flip <- function(data) {
  data %>%
    filter(term %in% c("treatmentcontrol", "treatmentfactcheck")) %>%
    mutate(
      estimate = if_else(term == "treatmentcontrol",-1 * estimate, estimate),
      conf.low = if_else(term == "treatmentcontrol",-1 * conf.low, conf.low),
      conf.high = if_else(term == "treatmentcontrol",-1 * conf.high, conf.high),
    )
  
}
filter_and_flip <- function(data) {
  data %>%
    filter(term %in% c("treatmentcontrol", "treatmentfactcheck")) %>%
    mutate(
      estimate = if_else(term == "treatmentcontrol",-1 * estimate, estimate),
      conf.low = if_else(term == "treatmentcontrol",-1 * conf.low, conf.low),
      conf.high = if_else(term == "treatmentcontrol",-1 * conf.high, conf.high),
    )
  
}

fits <- list()
for(i in 1:nrow(targets_long)) {
  if (!is.na(targets_long$target_of_misinformation[i])) {
    dv <- targets_long$target_of_misinformation[i]
    fc <- targets_long$fc[i]
    topic_short_2 <- targets_long$topic_short_2[i]
    
    
    formula <- formula(paste0("`", dv, "` ~ treatment +", covariates))
    fit <-
      lm_robust(
        formula = formula,
        data = filter(all_panels_long, fc == fc, topic_short_2 == topic_short_2)
      )
    
    fits[[i]] <-
      fit %>%
      tidy %>%
      mutate(dv = dv,
             fc = fc,
             topic_short_2 = topic_short_2) %>%
      filter_and_flip
  }
  
}



ft_targets_ates_w1_all_adj <- bind_rows(fits)



ft_targets_meta_w1_all_adj <- 
  ft_targets_ates_w1_all_adj %>% 
  mutate(term2 = term) %>% 
  group_by(term2) %>% 
  do(tidy(rma.uni(yi = estimate, sei = std.error, data = .), conf.int = TRUE)) %>% 
  mutate(term = term2, outcome = "META")

# by party

Dfits <- list()
Rfits <- list()
Ifits <- list()
for(i in 1:nrow(targets_long)) {
  if (!is.na(targets_long$target_of_misinformation[i])) {
    dv <- targets_long$target_of_misinformation[i]
    fc <- targets_long$fc[i]
    topic_short_2 <- targets_long$topic_short_2[i]
    
    
    formula <- formula(paste0("`", dv, "` ~ treatment +", covariates_no_pid))
    Dfit <-
      lm_robust(
        formula = formula,
        data = filter(all_panels_long, fc == fc, topic_short_2 == topic_short_2, lucid_pid_3 == "Democrat")
      )
    
    Rfit <-
      lm_robust(
        formula = formula,
        data = filter(all_panels_long, fc == fc, topic_short_2 == topic_short_2, lucid_pid_3 == "Republican")
      )
    
    Ifit <-
      lm_robust(
        formula = formula,
        data = filter(all_panels_long, fc == fc, topic_short_2 == topic_short_2, lucid_pid_3 == "Independent")
      )
    
    Dfits[[i]] <-
      Dfit %>%
      tidy %>%
      mutate(dv = dv,
             fc = fc,
             topic_short_2 = topic_short_2,
             lucid_pid_3 = "Democrat") %>%
      filter_and_flip
    
    
    Rfits[[i]] <-
      Rfit %>%
      tidy %>%
      mutate(dv = dv,
             fc = fc,
             topic_short_2 = topic_short_2,
             lucid_pid_3 = "Republican") %>%
      filter_and_flip
    
    Ifits[[i]] <-
      Ifit %>%
      tidy %>%
      mutate(dv = dv,
             fc = fc,
             topic_short_2 = topic_short_2,
             lucid_pid_3 = "Independent") %>%
      filter_and_flip
    
  }
  
}

ft_targets_ates_w1_PID_adj <- bind_rows(Dfits, Rfits, Ifits)

ft_targets_meta_w1_PID_adj <- ft_targets_ates_w1_PID_adj %>% 
  mutate(term2 = term) %>% 
  group_by(term2, lucid_pid_3) %>% 
  do(tidy(rma.uni(yi = estimate, sei = std.error, data = .), conf.int = TRUE)) %>% 
  mutate(term = term2, outcome = "META")

# reassignment

t0 <- ft_targets_ates_w1_PID_adj
t1 <- ft_targets_meta_w1_PID_adj
t2 <- ft_targets_ates_w1_all_adj
t3 <- ft_targets_meta_w1_all_adj

t0 %<>% 
  bind_rows(
    t2 %>% 
      mutate(
        lucid_pid_3 = "Overall"
      )
  ) %>% 
  mutate(
    lucid_pid_3 = lucid_pid_3 %>% 
      factor(
        c(
          "Democrat",
          "Independent",
          "Republican",
          "Overall"
        )
      )
  )

t1 %<>% 
  bind_rows(
    t3 %>% 
      mutate(
        lucid_pid_3 = "Overall"
      )
  ) %>% 
  mutate(
    lucid_pid_3 = lucid_pid_3 %>% 
      factor(
        c(
          "Democrat",
          "Independent",
          "Republican",
          "Overall"
        )
      )
  )

t0$term %<>% 
  str_detect("control") %>% 
  ifelse(
    "Misinformation effect",
    "Correction effect"
  ) %>% 
  factor(
    c("Misinformation effect",
      "Correction effect")
  )

t1$term %<>% 
  str_detect("control") %>% 
  ifelse(
    "Misinformation effect",
    "Correction effect"
  ) %>% 
  factor(
    c("Misinformation effect",
      "Correction effect")
  )

t0$sig <- t0$p.value %>% 
  is_less_than(.05) %>% 
  ifelse(
    "p <.05", "p >= .05"
  )

# label significant effects

t4 <- t0 %>% 
  filter(
    p.value <= .05
  ) 

t4$lab <- t4$dv %>% 
  plyr::mapvalues(
    c("w1_ft_kamala_harris", "w1_ft_the_world_health_organization_(who)"),
    c("Kamala Harris", "WHO")
  )


t1$lab <- t1$estimate %>% 
  round(2) %>% 
  str_replace(fixed("0."), fixed("."))

t1$lab %<>% 
  str_detect("-") %>% 
  ifelse(
    t1$lab %>% 
      str_pad(width = 4, side = "right", pad = "0"),
    t1$lab %>% 
      str_pad(width = 3, side = "right", pad = "0")
  )

t1$lab %<>% 
  str_c(
    c("***", "**", "*", "") %>% 
      extract(
        t1$p.value %>% 
          findInterval(
            c(-Inf, .001, .01, .05, Inf)
          )
      )
  )


t0 %>% 
  ggplot() +
  geom_hline(
    yintercept = 0,
    linetype = "dashed"
  ) +
  geom_dotplot(
    aes(x = lucid_pid_3, y = estimate, 
        fill = sig
    ), 
    binwidth = .25, 
    # shape = 21,
    binaxis = "y",
    stackdir = "center",
    binpositions= "all", 
    # size = 5,
    dotsize = .8,
    # abs_size = T
  ) +
  geom_segment(
    aes(
      x = lucid_pid_3,
      xend = lucid_pid_3,
      y = conf.low,
      yend = conf.high
    ),
    size = 1,
    data = t1
  ) +
  geom_point(
    aes(
      x = lucid_pid_3,
      y = estimate
    ),
    data = t1,
    shape = 23,
    size = 9.5,
    fill = "white"
  ) +
  geom_text(
    aes(
      lucid_pid_3, estimate, label = lab
    ),
    size = 2.75,
    data = t1
  ) +
  geom_text(
    aes(
      y = estimate, 
      label = lab %>% str_replace(" ", "\n"),
      x = lucid_pid_3
    ),
    position = position_nudge(x = .075),
    data = t4,
    fontface = "italic",
    size = 2.5,
    lineheight = .7, 
    vjust = 0
  ) +
  coord_flip() +
  labs(
    x = "",
    y = "Difference on 100 point feeling thermometer"
  ) +
  facet_wrap(~term, nrow = 1) +
  scale_fill_grey(start = .3, end = 1)